package com.mxm.mirai.web;


import com.mxm.mirai.config.MXM_config;
import com.mxm.mirai.dao.Mail;
import com.mxm.mirai.service.MailService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;

import java.security.GeneralSecurityException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 漫小猫
 * @since 2020-02-03
 */
@RestController
public class MailWeb {

    @Autowired
    MailService mailService;

    @GetMapping("/selectmail")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @ApiOperation(value = "邮箱信息获取", notes = "拥有 admin 权限可以访问该页面")
    public Mail SelectMail(){
       return mailService.SelectMail();
    }
    @PostMapping(value = "/updatemail")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @ApiOperation(value = "邮箱信息更新", notes = "拥有 admin 权限可以访问该页面")
    public Integer UpdateMail(Mail mail){
        return mailService.UpdateMail(mail);
    }
    @PostMapping("/mailtest")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    @ApiOperation(value = "邮箱测试", notes = "拥有 admin 权限可以访问该页面")
    public Integer MailTest(Mail mail){
        String personal = "漫小猫";
        String title = "测试信息";
        String text = "如果您收到了此邮件，就表邮件发送测试成功！";
        try {
             if (MXM_config.bottools.ToMail(mail.getSmtp(),mail.getNumber(),mail.getPassword(),mail.getPort(),personal,mail.getLetter(),title,text)){
                 return 1;
             }
             return 0;
        } catch (Exception e) {
            return 0;
        }
    }
}

